#include "at32f403a_407_wk_config.h"


/**
  * @brief  system clock config program
  * @note   the system clock is configured as follow:
  *         system clock (sclk)   = hick / 12 * pll_mult
  *         system clock source   = HICK_VALUE
  *         - sclk                = 180000000
  *         - ahbdiv              = 1
  *         - ahbclk              = 180000000
  *         - apb1div             = 2
  *         - apb1clk             = 90000000
  *         - apb2div             = 2
  *         - apb2clk             = 90000000
  *         - pll_mult            = 45
  *         - pll_range           = GT72MHZ (greater than 72 mhz)
  * @param  none
  * @retval none
  */
void wk_system_clock_config(void)
{
  /* reset crm */
  crm_reset();

  /* enable lick */
  crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);

  /* wait till lick is ready */
  while(crm_flag_get(CRM_LICK_STABLE_FLAG) != SET)
  {
  }

  /* enable hick */
  crm_clock_source_enable(CRM_CLOCK_SOURCE_HICK, TRUE);

  /* wait till hick is ready */
  while(crm_flag_get(CRM_HICK_STABLE_FLAG) != SET)
  {
  }

  /* config pll clock resource */
  crm_pll_config(CRM_PLL_SOURCE_HICK, CRM_PLL_MULT_45, CRM_PLL_OUTPUT_RANGE_GT72MHZ);

  /* enable pll */
  crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);

  /* wait till pll is ready */
  while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
  {
  }

  /* config ahbclk */
  crm_ahb_div_set(CRM_AHB_DIV_1);

  /* config apb2clk, the maximum frequency of APB2 clock is 120 MHz  */
  crm_apb2_div_set(CRM_APB2_DIV_2);

  /* config apb1clk, the maximum frequency of APB1 clock is 120 MHz  */
  crm_apb1_div_set(CRM_APB1_DIV_2);

  /* enable auto step mode */
  crm_auto_step_mode_enable(TRUE);

  /* select pll as system clock source */
  crm_sysclk_switch(CRM_SCLK_PLL);

  /* wait till pll is used as system clock source */
  while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
  {
  }

  /* disable auto step mode */
  crm_auto_step_mode_enable(FALSE);

  /* update system_core_clock global variable */
  system_core_clock_update();
}

/**
  * @brief  config periph clock
  * @param  none
  * @retval none
  */
void wk_periph_clock_config(void)
{
    /* enable iomux periph clock */
    crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);

    /* enable gpioa periph clock */
    crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);

    /* enable gpiob periph clock */
    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

    /* enable gpioc periph clock */
    crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);

    /* enable gpiod periph clock */
    crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);

    /* enable acc periph clock */
    crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);

    /* enable tmr3 periph clock */
    crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);

    /* enable usb periph clock */
    crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
    
    /* enable tmr6 periph clock */
    crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, TRUE);
    
    /* enable spi2 periph clock */
    crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
}


void wk_periph_clock_disable_config(void)
{
    /* enable iomux periph clock */
    crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, FALSE);

    /* enable gpioa periph clock */
    crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, FALSE);

    /* enable gpiob periph clock */
    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, FALSE);

    /* enable gpioc periph clock */
    crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, FALSE);

    /* enable gpiod periph clock */
    crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, FALSE);

    /* enable acc periph clock */
    crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, FALSE);

    /* enable tmr3 periph clock */
    crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, FALSE);

    /* enable usb periph clock */
    crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, FALSE);
    
    /* enable tmr6 periph clock */
    crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, FALSE);
    
    /* enable spi2 periph clock */
    crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, FALSE);
}

/**
  * @brief  nvic config
  * @param  none
  * @retval none
  */
void wk_nvic_config(void)
{
    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);

    NVIC_SetPriority(MemoryManagement_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
    NVIC_SetPriority(BusFault_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
    NVIC_SetPriority(UsageFault_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
    NVIC_SetPriority(SVCall_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
    NVIC_SetPriority(DebugMonitor_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
    NVIC_SetPriority(PendSV_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));
    NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
    nvic_irq_enable(USBFS_L_CAN1_RX0_IRQn, 0, 0);
}

void wk_nvic_disable_config(void)
{
    nvic_irq_disable(USBFS_L_CAN1_RX0_IRQn);
}

/* add user code begin 1 */


/* add user code end 1 */
